﻿Public Class frmNetAddSwitch

    Public Result As MsgBoxResult

    Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
        Call SanitizeInput()
        If ValidateInput() Then
            Result = MsgBoxResult.Ok
            Me.Hide()
        End If
    End Sub
    Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click
        Result = MsgBoxResult.Cancel
        Me.Hide()
    End Sub

    Private Sub frmNetAddSwitch_Activated(sender As Object, e As System.EventArgs) Handles Me.Activated
        txtDisplayName.Focus()
    End Sub

    Private Sub SanitizeInput()
        txtDisplayName.Text = txtDisplayName.Text.Trim
        txtIPAddressHostname.Text = txtIPAddressHostname.Text.Trim
        txtPort.Text = txtPort.Text.Trim
        txtPassword.Text = txtPassword.Text.Trim
        txtUserName.Text = txtUserName.Text.Trim
    End Sub

    Private Function ValidateInput() As Boolean
        If frmBrowser.NameExists(txtDisplayName.Text) Then
            InvMsg("Display Name already exists within your network tree.")
            txtDisplayName.Focus()
            Return False
        End If
        If txtDisplayName.Text = "" Then
            InvMsg("Display Name cannot be empty.")
            txtDisplayName.Focus()
            Return False
        End If
        If txtIPAddressHostname.Text = "" Then
            InvMsg("You must enter an IP or a hostname.")
            txtIPAddressHostname.Focus()
            Return False
        End If
        If txtPort.Text = "" Or (Not IsNumeric(txtPort.Text)) Or Integer.Parse(txtPort.Text) > 65535 Or Integer.Parse(txtPort.Text) < 1 Then
            InvMsg("You must enter a valid port.")
            txtPort.Focus()
            Return False
        End If
        If txtUserName.Text = "" Then
            InvMsg("You must enter an SSH user name.")
            txtUserName.Focus()
            Return False
        End If
        Return True
    End Function

    Private Sub InvMsg(Message As String)
        MsgBox(Message, MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly, "PurpleKnife")
    End Sub
End Class